// import store from 'store'
import store from '@/tools/storage'
import * as types from '@/store/mutation-types'
import { BASE_URL, TOKEN_URL } from '@/config/const'
import { SUCCESS_STATUS } from '@/config/status'
import axiosObj from '@/tools/axiosObj'
import api from '@/tools/api'
import promise from '@/tools/promise'

const state = {
  token: '',
  authList: [],
  authRoleList: [],
  list: [],
  detail: {},
  pageData: {
    total: 1,
    per_page: 15,
    current_page: 1,
    last_page: 1
  },
  scrollTop: 0,
  loginAuthCode: '',
  forgetLoginPasswordAuthCode: {},
  joinUsAuthCode: {}
}

// mutations
const mutations = {
  [types.GET_TOKEN] (state, payload) {
    if (payload.token.length === 32) {
      state.token = payload.token
      store.set(types.TOKEN, payload.token)
    } else {
      alert('token err: ' + payload.token)
    }
  },
  [types.CLEAR_TOKEN] (state) {
    state.token = ''
    store.clearAll()
  },
  [types.GET_AUTH_LIST] (state, payload) {
    state.authList = payload
    store.set(types.AUTH_LIST, payload)
  },
  [types.SCOROLL_TOP] (state, payload) {
    state.scrollTop = payload
  },
  [types.CREATE_LOGIN_AUTH_CODE] (state, payload) {
    state.loginAuthCode = payload
  },
  [types.CLEAR_LOGIN_AUTH_CODE] (state) {
    state.loginAuthCode = {}
  },
  // 后台获取权限分页列表数据
  [types.GET_LIST] (state, payload) {
    state.list = payload.data
    state.pageData.total = Number(payload.total)
    state.pageData.per_page = Number(payload.per_page)
    state.pageData.current_page = Number(payload.current_page)
    state.pageData.last_page = Number(payload.last_page)
  },
  // 后台获取某条权限详情
  [types.GET_AUTH_DETAIL] (state, payload) {
    state.detail = payload
  },
  // 获取某权限下的 拥有该权限的角色和未拥有该权限的角色
  [types.GET_AUTH_ROLE_LIST] (state, payload) {
    state.authRoleList = payload
  }
}

// actions
const actions = {
  // 获取TOKEN
  async getToken ({ commit }) {
    let token = store.get(types.TOKEN)
    if (!token) {
      let tokenRes = await axiosObj.post(BASE_URL + TOKEN_URL)
      token = tokenRes.data.res[0].res.token
    }
    commit(types.GET_TOKEN, {token: token})
  },
  // 清除TOKEN
  clearToken ({ commit }) {
    commit(types.CLEAR_TOKEN)
  },
  // 根据token获取当前的角色集对应的权限列表
  async getAuthList ({ commit }, payload = {getServiceAuth: false}) {
    let authList = []
    if (payload.getServiceAuth === true) {
      let listRes = await api('serveruser/user/User/getAuthByToken')
      authList = listRes.res
      SUCCESS_STATUS === listRes.status && commit(types.GET_AUTH_LIST, authList)
    } else {
      authList = store.get(types.AUTH_LIST)
    }
  },
  // 获取当前滚动条的位置
  scorollTop ({ commit }) {
    if (document) {
      let top = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
      commit(types.SCOROLL_TOP, top)
    }
  },
  // 获取登录时图形验证码
  async createLoginAuthCode ({ commit }, payload) {
    payload = {
      type: 'login',
      code_id: payload.code_id,
      fontSize: 20,
      useCurve: 1,
      imageH: 38,
      imageW: 140,
      length: 3
    }
    let codeRes = await api('serverauthcode/authcode/ApiAuthCode/createAuthCode', payload)
    SUCCESS_STATUS === codeRes.status && commit(types.CREATE_LOGIN_AUTH_CODE, codeRes.res.code_base64)
    // return promise(codeRes)
  },
  // 清除当前图形验证码
  clearLoginAuthCode ({ commit }) {
    commit(types.CREATE_LOGIN_AUTH_CODE)
  },
  // 发送手机验证码
  async sendPhoneCode ({ commit }, payload) {
    let sendRes = await api('serverindex/index/Index/sendVerify', payload)
    return promise(sendRes)
  },
  // 后台获取权限分页列表数据
  async getList ({ commit }, payload) {
    let config = {loading: true, loadingText: '正在获取权限分页数据'}
    let getListRes = await api('serverauth/auth/Auth/list', payload, config)
    SUCCESS_STATUS === getListRes.status && commit(types.GET_LIST, getListRes.res)
  },
  // 后台进行添加一条数据
  async addAuth ({ commit }, payload) {
    let config = {loading: true, loadingText: '正在添加一条权限'}
    let addRes = await api('serverauth/auth/Auth/add', payload, config)
    return promise(addRes)
  },
  // 后台获取某一条权限详情
  async authInfo ({ commit }, payload) {
    let config = {loading: true, loadingText: '正在获取权限详情'}
    let getInfoRes = await api('serverauth/auth/Auth/authInfo', payload, config)
    SUCCESS_STATUS === getInfoRes.status && commit(types.GET_AUTH_DETAIL, getInfoRes.res)
  },
  // 后台进行编辑某一条权限
  async editAuth ({ commit }, payload) {
    let config = {loading: true, loadingText: '正在编辑该条权限...'}
    let editRes = await api('serverauth/auth/Auth/edit', payload, config)
    return promise(editRes)
  },
  // 启用某个权限
  async startAuth ({ commit }, payload) {
    let startRes = await api('serverauth/auth/Auth/start', payload)
    return promise(startRes)
  },
  // 禁用某个权限
  async stopAuth ({ commit }, payload) {
    let stopRes = await api('serverauth/auth/Auth/stop', payload)
    return promise(stopRes)
  },
  // 获取某权限，拥有和不拥有该权限的角色列表
  async getAuthRoleList ({ commit }, payload) {
    let getListRes = await api('serverauth/auth/Auth/roleByAuth', payload)
    SUCCESS_STATUS === getListRes.status && commit(types.GET_AUTH_ROLE_LIST, getListRes.res)
  },
  // 给某个角色赋予某个权限
  async bindAuth ({ commit }, payload) {
    let bindAuthRes = await api('serverauth/auth/Auth/addAuthByRole', payload)
    return promise(bindAuthRes)
  },
  // 解除某个角色某个权限
  async unbindAuth ({ commit }, payload) {
    let unbindAuthRes = await api('serverauth/auth/Auth/removeAuthsByRole', payload)
    return promise(unbindAuthRes)
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
